/**
 * 
 */
package com.base.myproject.client.busi.service.analysis;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.PieDataProvider;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.PieChart;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;

/**
 * 当月销售冠军对比图。默认前10名
 * @author zgw
 *2009-11-15 上午09:02:54
 * javazgw@gmail.com
 */
public class TopsaleAnalysis  extends ContentPanel{

BaseStore<JDBCModelData> store;
	
	SelectionListener<ButtonEvent> listener ;
	
	int month=0;
	public TopsaleAnalysis() {
		init();
	}
	void init() {
		Date d = new Date();
		
		setWidth("400px");
		initlistener();
		initstore();
		initQueryParamPanel();
		initWidget();
	}
	
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("bcodeshortcode");

		type.addField("total");
	

		StoreInfo info = new StoreInfo();


		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info,false);

	}
	void initWidget() {
		ChartModel model = new ChartModel("月营业额技师创利饼图分析",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		
		  PieChart pie = new PieChart();  
	      pie.setAlpha(0.5f);  
	      pie.setTooltip("#label# $#val#<br>#percent#");  
	      
	      PieDataProvider pieprovider = new PieDataProvider("total","bcodeshortcode"); 
	      pieprovider.bind(store);
	      pie.setDataProvider(pieprovider);
	      
	      pie.setAnimate(false);  
	      pie.setAlphaHighlight(true);  
	      pie.setGradientFill(true);  
	      model.addChartConfig(pie);
	      
	      
	      
		 String url = "chart/open-flash-chart.swf";
		 final Chart chart = new Chart(url);
			// chart.setToolTip("点击产品的图型看看");
			chart.setChartModel(model);
			chart.setHeight("300px");
			add(chart);
	}
	
	void initQueryParamPanel() {
		Button premonth = new Button("上月");
		premonth.addSelectionListener(listener);
		Button nextmonth = new Button("下月");
		nextmonth.addSelectionListener(listener);
		Button thismonth = new Button("本月");
		thismonth.addSelectionListener(listener);
		this.addButton(premonth);
		this.addButton(nextmonth);
		this.addButton(thismonth);
	}
	 void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					if(ce.getButton().getText().equals("上月"))
					{
						month--;
					}
					else if(ce.getButton().getText().equals("下月"))
					{
						month++;
					}
						else if(ce.getButton().getText().equals("本月"))
						{
							month=0;
							
						}
					//if(ce.getButton().getText().equals("本周"))
					{
						
						StoreInfo info = store.getStoreinfo();
						String str = "";
						
						
						str += "select  bcodeshortcode,sum(isnull(total,0)) as total from s_service_po_g  ";
						str += "where exists (select * from s_service_po where s_service_po.icode = s_service_po_g.upicode and datepart(month,s_service_po.createdate)=datepart(month,getDate())%2B"+month+") ";
						str += "and bcodeshortcode is not null ";
						str += "group by bcodeshortcode ";
					
						info.setSql(str);
						store.reload();
						
						
					}
				}
		 };
		 
	 }
}
